package com.wang.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.pojo.InfoEnum;
import com.wang.common.service.BaseService;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.dao.IAASensitiveWordsDao;
import com.wang.dao.IAEmailDao;
import com.wang.dao.IAResultDao;
import com.wang.dao.impl.AASensitiveWordsDaoImpl;
import com.wang.dao.impl.AEmailDaoImpl;
import com.wang.dao.impl.AResultDaoImpl;
import com.wang.pojo.AEmail;
import com.wang.pojo.AEmailEnum;
import com.wang.pojo.AResult;
import com.wang.pojo.AResultEnum;
import com.wang.pojo.ASensitiveWords;
import com.wang.service.IEmailDbService;

/**
 * 用户模块Service的实现类
 * 
 * @author YH
 *
 */
public class EmailDbServiceImpl extends BaseService implements IEmailDbService {
	private IAEmailDao emailDao = new AEmailDaoImpl();
	private IAASensitiveWordsDao asensitiveWordsDao = new AASensitiveWordsDaoImpl();
	private IAResultDao aresultDao = new AResultDaoImpl();

	@Override
	public JSONObject saveOneEmailService(AEmail email) {
		JSONObject resultJson = new JSONObject();
		email.setTag(this.regexUtil.randomSn());
		email.setStatus(AEmailEnum.STATUS_UNPATCH.getCode());
		email.setCreateTime(new Date());
		email.setUpdateTime(new Date());
		if(email.getPubTime() == null) {
			email.setPubTime(new Date());
		}
		/* 调用dao保存数据库 */
		int res = this.emailDao.saveOne(email);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), email.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject saveBatchEmailService(List<AEmail> emailList) {
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int[] res = this.emailDao.saveBatch(emailList);
		resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
		resultJson.put(InfoEnum.VAR_INFO.getInfo(),
				ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
		JSONObject dataJson = new JSONObject();
		dataJson.put(InfoEnum.VAR_TOTAL.getInfo(), emailList.size());
		dataJson.put(InfoEnum.VAR_SUCCED.getInfo(), res.length);
		dataJson.put(InfoEnum.VAR_FAILED.getInfo(), 0);

		resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		return resultJson;
	}

	@Override
	public JSONObject updateOneEmailService(AEmail email) {
		JSONObject resultJson = new JSONObject();
		email.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.emailDao.updateOne(email);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), email.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneEmailService(Map<String, Object> condMap) {
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.emailDao.deleteOne(condMap);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public AEmail findOneEmailService(Map<String, Object> condMap) {
		AEmail one = this.emailDao.findOne(condMap);

		return one;
	}

	@Override
	public List<AEmail> findCondListEmailService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<AEmail> list = this.emailDao.findCondList(pageInfoUtil, condMap);

		return list;
	}

	@Override
	public JSONObject saveOneSensitiveWordsService(ASensitiveWords asensitiveWords) {
		JSONObject resultJson = new JSONObject();
		asensitiveWords.setCreateTime(new Date());
		asensitiveWords.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.asensitiveWordsDao.saveOne(asensitiveWords);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), asensitiveWords.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneSensitiveWordsService(ASensitiveWords asensitiveWords) {
		JSONObject resultJson = new JSONObject();
		asensitiveWords.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.asensitiveWordsDao.updateOne(asensitiveWords);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), asensitiveWords.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneSensitiveWordsService(Map<String, Object> condMap) {
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.asensitiveWordsDao.deleteOne(condMap);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public ASensitiveWords findOneSensitiveWordsService(Map<String, Object> condMap) {
		ASensitiveWords one = this.asensitiveWordsDao.findOne(condMap);

		return one;
	}

	@Override
	public List<ASensitiveWords> findCondListSensitiveWordsService(PageInfoUtil pageInfoUtil,
			Map<String, Object> condMap) {
		List<ASensitiveWords> list = this.asensitiveWordsDao.findCondList(pageInfoUtil, condMap);

		return list;
	}

	/* =======================AResult======================== */
	@Override
	public JSONObject saveOneResultService(AResult result) {
		JSONObject resultJson = new JSONObject();
		result.setStatus(AResultEnum.STATUS_ENABLED.getCode());
		result.setCreateTime(new Date());
		result.setUpdateTime(new Date());
		if(result.getPubTime() == null) {
			result.setPubTime(new Date());
		}
		/* 调用dao保存数据库 */
		int res = this.aresultDao.saveOne(result);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), result.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneResultService(AResult aresult) {
		JSONObject resultJson = new JSONObject();
		aresult.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.aresultDao.updateOne(aresult);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), aresult.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneResultService(Map<String, Object> condMap) {
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.aresultDao.deleteOne(condMap);
		if (res > 0) {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));

			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);

			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		} else {
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(),
					ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public AResult findOneResultService(Map<String, Object> condMap) {
		AResult findOne = aresultDao.findOne(condMap);
		return findOne;
	}

	@Override
	public List<AResult> findCondListResultService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<AResult> findCondList = aresultDao.findCondList(pageInfoUtil, condMap);
		return findCondList;
	}
}
